package com.kcsoft.welcome.controller;

import com.kcsoft.welcome.pojo.NewMem;
import com.kcsoft.welcome.pojo.PageBean;
import com.kcsoft.welcome.pojo.Result;
import com.kcsoft.welcome.service.NewMemService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/newmem")
public class NewMemController {

    @Autowired
    NewMemService newMemService;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody NewMem newMem) {

        if (newMem.getName() == null || newMem.getName().equals("")) {
            return Result.error("姓名不能为空");
        }

        if (newMem.getPhone() == null || newMem.getPhone().equals("")) {
            return Result.error("手机号不能为空");
        }

        if (newMem.getCla() == null || newMem.getCla().equals("")) {
            return Result.error("班级不能为空");
        }

        if(newMem.getPr() == null || newMem.getPr().equals("")) {
            return Result.error("专业不能为空");
        }

        if (newMem.getXueyuan() == null || newMem.getXueyuan().equals("")) {
            return Result.error("学院不能为空");
        }
        newMemService.add(newMem);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<PageBean<NewMem>> list(Integer pageNum, Integer pageSize) {
        PageBean<NewMem> pb = newMemService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @GetMapping("/deleteAll")
    public Result deleteAll() {
        newMemService.deleteAll();
        return Result.success();
    }

}
